import { createPopupSlot } from '@/services/popupSlot'
import { IEventTemplateReq } from './interface'

export const popupSlot = createPopupSlot()

export const normalizeReqData = <T extends IEventTemplateReq | null>(reqData: T): T => {
  if (reqData) {
    if (reqData.searchWord) {
      const newSearchWord = reqData.searchWord.trim()
      if (reqData.searchWord !== newSearchWord) {
        reqData = { ...reqData, searchWord: newSearchWord }
      }
    }
    if (reqData.enableFlag === -1) {
      reqData = { ...reqData, enableFlag: reqData.enableFlag === -1 ? undefined : reqData.enableFlag }
    }
    if (reqData.visibleFlag === -1) {
      reqData = { ...reqData, visibleFlag: reqData.visibleFlag === -1 ? undefined : reqData.visibleFlag }
    }
    if (reqData.shipTrackRequire === -1) {
      reqData = { ...reqData, shipTrackRequire: reqData.shipTrackRequire === -1 ? undefined : reqData.shipTrackRequire }
    }
  }
  return reqData
}

export const getShipTrackStatusName = Object.assign(
  (value: number | null | undefined) => {
    switch (value) {
      case 1:
        return '必须' as const
      case 2:
        return '可选' as const
      default:
        return `${value ?? ''}`
    }
  },
  { valueList: [1, 2] },
)

export const getMessageTypeName = Object.assign(
  (value: number | null | undefined) => {
    switch (value) {
      case 1:
        return 'Information' as const
      case 2:
        return 'Notification' as const
      case 3:
        return 'Alert' as const
      default:
        return `${value ?? ''}` || null
    }
  },
  { valueList: [1, 2, 3] },
)

export const getVisibleFlagName = Object.assign(
  (value: number | null | undefined) => {
    switch (value) {
      case 1:
        return '是' as const
      case 0:
        return '否' as const
      default:
        return `${value ?? ''}` || null
    }
  },
  { valueList: [1, 0] },
)

export const getEventStatusName = Object.assign(
  (value: number | null | undefined) => {
    switch (value) {
      case 1:
        return '必须' as const
      case 2:
        return '可选' as const
      default:
        return `${value ?? ''}` || null
    }
  },
  { valueList: [1, 2] },
)

export const getTabIdName = Object.assign(
  (value: number | null | undefined) => {
    switch (value) {
      case 0:
        return '全部' as const
      case 1:
        return '启用' as const
      case 2:
        return '禁用' as const
      default:
        return `${value ?? ''}` || null
    }
  },
  { valueList: [0, 1, 2] },
)

export const getStatusListOption = {
  1: [
    {
      label: '待处理',
      value: 11,
    },
    {
      label: '待发货',
      value: 12,
    },
    {
      label: '待离港',
      value: 13,
    },
    {
      label: '运输中',
      value: 14,
    },
    {
      label: '待提取',
      value: 15,
    },
    {
      label: '中转中',
      value: 16,
    },
    {
      label: '派送中',
      value: 17,
    },
    {
      label: '已签收',
      value: 18,
    },
    {
      label: '已取消',
      value: 19,
    },
  ],
  2: [
    {
      label: '待审核',
      value: 21,
    },
    {
      label: '待申报',
      value: 22,
    },
    {
      label: '申报中',
      value: 23,
    },
    {
      label: '查验中',
      value: 24,
    },
    {
      label: '已放行',
      value: 25,
    },
    {
      label: '已退单',
      value: 26,
    },
    {
      label: '已取消',
      value: 27,
    },
  ],
  3: [
    {
      label: '待审核',
      value: 31,
    },
    {
      label: '待申报',
      value: 32,
    },
    {
      label: '申报中',
      value: 33,
    },
    {
      label: '待支付关税',
      value: 34,
    },
    {
      label: '查验中',
      value: 35,
    },
    {
      label: '已放行',
      value: 36,
    },
    {
      label: '已退运',
      value: 37,
    },
    {
      label: '已取消',
      value: 38,
    },
  ],
  4: [
    {
      label: '待预约',
      value: 41,
    },
    {
      label: '待预约',
      value: 42,
    },
    {
      label: '待确认',
      value: 43,
    },
    {
      label: '已确认',
      value: 44,
    },
    {
      label: '已签收',
      value: 45,
    },
    {
      label: '已回传',
      value: 46,
    },
    {
      label: '已拒收',
      value: 47,
    },
    {
      label: '已取消',
      value: 48,
    },
  ],
  5: [
    {
      label: '待处理',
      value: 51,
    },
    {
      label: '待投保',
      value: 52,
    },
    {
      label: '已投保',
      value: 53,
    },
    {
      label: '已拒绝',
      value: 54,
    },
    {
      label: '已取消',
      value: 55,
    },
  ],
  6: [
    {
      label: '待分配',
      value: 61,
    },
    {
      label: '调查中',
      value: 62,
    },
    {
      label: '处理中',
      value: 63,
    },
    {
      label: '已解决',
      value: 64,
    },
    {
      label: '已关闭',
      value: 65,
    },
    {
      label: '已取消',
      value: 66,
    },
  ],
}

export const getFirstLineName = Object.assign(
  (value: number | null | undefined) => {
    switch (value) {
      case 1:
        return 'OCEAN' as const
      case 2:
        return 'AIR' as const
      case 3:
        return 'GROUND_TRUCK' as const
      case 4:
        return 'GROUND_TRAIN' as const
      default:
        return `${value ?? ''}` || null
    }
  },
  { valueList: [1, 2, 3, 4] },
  {
    colorMap: {
      1: 'magenta',
      2: 'cyan',
      3: 'gold',
      4: 'lime',
    } as Record<1 | 2 | 3 | 4, string>,
    colorOf: (value?: number | null) => (value != null ? getFirstLineName.colorMap[value as 1 | 2 | 3 | 4] : undefined),
  },
)

export const getTailLineName = Object.assign(
  (value: number | null | undefined) => {
    switch (value) {
      case 1:
        return '快递' as const
      case 2:
        return '卡车' as const
      case 3:
        return 'Amazon Freight' as const
      default:
        return `${value ?? ''}` || null
    }
  },
  { valueList: [1, 2, 3] },
  {
    colorMap: {
      1: 'magenta',
      2: 'cyan',
      3: 'gold',
    } as Record<1 | 2 | 3, string>,
    colorOf: (value?: number | null) => (value != null ? getTailLineName.colorMap[value as 1 | 2 | 3] : undefined),
  },
)

// 1-始发地 2-集货仓 3-起运港 4-目的港 5-中转仓 6-目的地
export const getLocationTypeName = Object.assign(
  (value: number | null | undefined) => {
    switch (value) {
      case 1:
        return '始发地' as const
      case 2:
        return '集货仓' as const
      case 3:
        return '起运港' as const
      case 4:
        return '目的港' as const
      case 5:
        return '中转仓' as const
      case 6:
        return '目的地' as const
      default:
        return `${value ?? ''}` || null
    }
  },
  { valueList: [1, 2, 3, 4, 5, 6] },
)

export const getEnableFlagName = Object.assign(
  (value: number | null | undefined) => {
    switch (value) {
      case 1:
        return '是' as const
      case 0:
        return '否' as const
      default:
        return `${value ?? ''}` || null
    }
  },
  { valueList: [1, 0] },
)
